package com.service.wmy.impl;

import com.dao.*;
import com.entity.*;
import com.service.wmy.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private ServiceDao serviceDao;
    @Resource
    private ServicestatusDao servicestatusDao;
    @Resource
    private ClientDao clientDao;
    @Resource
    private ServicetypeDao servicetypeDao;
    @Resource
    private OwnershipDao ownershipDao;//所有权
    @Resource
    private CloseregistrationDao closeregistrationDao;//查封登记
    @Resource
    private DissentingregistrationDao dissentingregistrationDao;//异议登记
    @Resource
    private OtherregistrationDao otherregistrationDao;//其他预告登记
    @Resource
    private ExistmortgageDao existmortgageDao;//他项权（现房抵押）
    @Resource
    private BeinmortgageDao beinmortgageDao;//他项权（在建抵押）
    @Resource
    private UserinfoDao userinfoDao;


    @Override
    public List<Order> getServiceList() {
        List<Order> orders = new ArrayList<>();

        List<com.entity.Service> services = serviceDao.selectByExample(null);

        for (com.entity.Service service: services) {
            //将需要的信息封装到order类中返回到前端
            Order order = new Order();
            order.setServicetype(servicetypeDao.selectByPrimaryKey(service.getServicetypeid()).getServicetype());
            order.setServicestatus(servicestatusDao.selectByPrimaryKey(service.getServicestatusid()).getServicestatus());
            order.setClientname(clientDao.selectByPrimaryKey(service.getProposer()).getClientname());
            order.setIdcard(clientDao.selectByPrimaryKey(service.getProposer()).getIdcard());

            OwnershipExample ownershipExample = new OwnershipExample();
            ownershipExample.createCriteria().andServicenumberEqualTo(service.getServiceid()+"");
            List<Ownership> ownerships = ownershipDao.selectByExample(ownershipExample);
            if (ownerships.size()!=0){
                order.setRegistertime(ownerships.get(0).getRegistertime());
                order.setUname(userinfoDao.selectByPrimaryKey(ownerships.get(0).getUid()).getUname());
            }

            //根据业务id在各种的登记表中查询出业务宗号为业务id的登记信息，取值放入封装信息类(order)
            CloseregistrationExample closeregistrationExample = new CloseregistrationExample();
            closeregistrationExample.createCriteria().andServiceidEqualTo(service.getServiceid());
            List<Closeregistration> closeregistrations = closeregistrationDao.selectByExample(closeregistrationExample);
            if (closeregistrations.size()!=0){
                order.setRegistertime(closeregistrations.get(0).getRegistertime());
                order.setUname(userinfoDao.selectByPrimaryKey(closeregistrations.get(0).getUid()).getUname());
            }

            DissentingregistrationExample dissentingregistrationExample = new DissentingregistrationExample();
            dissentingregistrationExample.createCriteria().andServiceidEqualTo(service.getServiceid());
            List<Dissentingregistration> dissentingregistrations = dissentingregistrationDao.selectByExample(dissentingregistrationExample);
            if(dissentingregistrations.size()!=0){
                order.setRegistertime(dissentingregistrations.get(0).getRegistertime());
                order.setUname(userinfoDao.selectByPrimaryKey(dissentingregistrations.get(0).getUid()).getUname());
            }

            OtherregistrationExample otherregistrationExample = new OtherregistrationExample();
            otherregistrationExample.createCriteria().andServiceidEqualTo(service.getServiceid());
            List<Otherregistration> otherregistrations = otherregistrationDao.selectByExample(otherregistrationExample);
            if(otherregistrations.size()!=0){
                order.setRegistertime(otherregistrations.get(0).getRegistertime());
                order.setUname(userinfoDao.selectByPrimaryKey(otherregistrations.get(0).getUid()).getUname());
            }

            ExistmortgageExample existmortgageExample = new ExistmortgageExample();
            existmortgageExample.createCriteria().andServiceidEqualTo(service.getServiceid());
            List<Existmortgage> existmortgages = existmortgageDao.selectByExample(existmortgageExample);
            if(existmortgages.size()!=0){
                order.setRegistertime(existmortgages.get(0).getRegistertime());
                order.setUname(userinfoDao.selectByPrimaryKey(existmortgages.get(0).getUid()).getUname());
            }
            orders.add(order);
        }
        return orders;
    }

    @Override
    public void addServiceAndclient(Integer aid, String proposername, String proposeridcard, String agentname, String agentidcard) {
        ClientExample proposer = new ClientExample();
        ClientExample.Criteria criteria = proposer.createCriteria();
        criteria.andIdcardEqualTo(proposeridcard);
        criteria.andClientnameEqualTo(proposername);
        List<Client> proposers = clientDao.selectByExample(proposer);
        //如果没有客户数据则添加客户，如果已有则用现有的客户数据
        if (proposers.size()==0){
            //根据申请人姓名和身份证号添加到客户表
            Client client = new Client();
            client.setClientname(proposername);
            client.setIdcard(proposeridcard);
            clientDao.insertSelective(client);
        }

        //根据身份证号获得客户信息，得到客户id填入业务表的申请人id中
        ClientExample clientExample = new ClientExample();
        clientExample.createCriteria().andIdcardEqualTo(proposeridcard);
        List<Client> clients = clientDao.selectByExample(clientExample);

        List<Client> clients1=null;
        //判断如果没有处理人就不添加到客户
        if (agentname!=""&&agentidcard!=""){
            ClientExample agent = new ClientExample();
            ClientExample.Criteria agentcriteria = agent.createCriteria();
            agentcriteria.andIdcardEqualTo(agentidcard);
            agentcriteria.andClientnameEqualTo(agentname);
            List<Client> agents = clientDao.selectByExample(agent);
            //如果没有客户数据则添加客户，如果已有则用现有的客户数据
            if (agents.size()==0){
                //根据代理人姓名和身份证号添加到客户表
                Client client1 = new Client();
                client1.setClientname(agentname);
                client1.setIdcard(agentidcard);
                clientDao.insertSelective(client1);
            }
            //根据身份证号获得客户信息，得到客户id填入业务表的代理人id中
            ClientExample clientExample1 = new ClientExample();
            clientExample1.createCriteria().andIdcardEqualTo(agentidcard);
            clients1 = clientDao.selectByExample(clientExample1);
        }

        //给业务表添加数据
        com.entity.Service service = new com.entity.Service();
        service.setServicetypeid(aid);
        String randomNum = randomNumber();
        service.setServicenumber(randomNum);
        service.setServicestatusid(1);
        service.setProposer(clients.get(0).getClientid());
        //没有处理人不填业务表处理人id
        if (clients1!=null) {
            service.setAgent(clients1.get(0).getClientid());
        }
        serviceDao.insertSelective(service);

        //新建登记表
        //获得业务id填入登记表外键中
        ServiceExample serviceExample = new ServiceExample();
        serviceExample.createCriteria().andServicenumberEqualTo(randomNum);
        List<com.entity.Service> services = serviceDao.selectByExample(serviceExample);
        Integer serviceid = services.get(0).getServiceid();
        if (aid==1){
            Ownership ownership = new Ownership();
            ownership.setServicenumber(randomNum);
            ownership.setRegistertime(new Date());
            ownershipDao.insertSelective(ownership);
        }
        if (aid==2){
            Otherregistration otherregistration = new Otherregistration();
            otherregistration.setServiceid(serviceid);
            otherregistration.setRegistertime(new Date());
            otherregistrationDao.insertSelective(otherregistration);
        }
        if (aid==3){
            Existmortgage existmortgage = new Existmortgage();
            existmortgage.setServiceid(serviceid);
            existmortgage.setRegistertime(new Date());
            existmortgageDao.insertSelective(existmortgage);
        }
        if (aid==4){
            Beinmortgage beinmortgage = new Beinmortgage();
            beinmortgage.setServiceid(serviceid);
            beinmortgage.setRegistertime(new Date());
            beinmortgageDao.insertSelective(beinmortgage);
        }
        if (aid==6){
            Closeregistration closeregistration = new Closeregistration();
            closeregistration.setServiceid(serviceid);
            closeregistration.setRegistertime(new Date());
            closeregistrationDao.insertSelective(closeregistration);
        }
        if (aid==7){
            Dissentingregistration dissentingregistration = new Dissentingregistration();
            dissentingregistration.setServiceid(serviceid);
            dissentingregistration.setRegistertime(new Date());
            dissentingregistrationDao.insertSelective(dissentingregistration);
        }
        if (aid==8){
            Closeregistration closeregistration = new Closeregistration();
            closeregistration.setServiceid(serviceid);
            closeregistration.setOpenregistertime(new Date());
            closeregistrationDao.insertSelective(closeregistration);
        }
    }

    public String randomNumber(){
       int i =  (int)(Math.random()*900)+100;
         String randomNumber =  (int)(new Date().getTime()) +""+ i ;
        return randomNumber.substring(1);

    }


}
